$(document).ready(function() {
	var height1 = $(window).height() - 55;
	$("#main_layout").attr("style", "margin-top: 5px;width:100%;height:" + height1 + "px");
	$("#main_layout").layout("resize", {
		width : "100%",
		height : height1 + "px"
	});
});

$(window).resize(function() {
	var height1 = $(window).height() - 55;
	$("#main_layout").attr("style", "margin-top: 5px;width:100%;height:" + height1 + "px");
	$("#main_layout").layout("resize", {
		width : "100%",
		height : height1 + "px"
	});
});

/**
 * 字典列表树
 */
$('#dic_tree').tree({
	url: "admin/dictionary/dic_getDicTreeJson.action",
	lines : true,
	onContextMenu: function (e,node) {
         e.preventDefault();
       $("#hidden_selectTreeNodeId").val(node.id);
       $(this).tree('select',node.target);
       if(node.id==0){
    	   $("#parentMenu").menu('show', {
               left: e.pageX,
               top: e.pageY
           });
       } else{
       $("#treeMenu").menu('show', {
             left: e.pageX,
             top: e.pageY
         });
         };
         
         
     },
     onClick : function (node) {
		if (node.id != 0) {
			$("#hidden_selectTreeNodeId").val(node.id);
			$("#dictionary_data").datagrid("load", {
				"dictionary.dicId" : node.id
			});
		}
	}
});

/*添加字典项*/
function addDictionary(){
	
	$('#dictionary_form_dialog').dialog({
        title: '字典--添加',
        iconCls: 'icon-book_addresses_add',
    });
	
    form_init();
    
    $('#dictionary_form').form({
        url: 'admin/dictionary/dic_saveOrUpdateDictionary.action',
        onSubmit: function() {
            return $(this).form('validate');
        },
        success: function(data) {
            var json = JSON.parse(data);
            if (json.result == "Success") {
                $('#dictionary_form_dialog').dialog('close');
                $.messager.alert('提示', json.message, 'info');
                //重新加载树
                $('#dic_tree').tree('reload'); 
            } else {
                $.messager.alert('错误', json.message, 'error');
            }
        }
    });
    $('#dictionary_form_dialog').dialog('open');
	
}

/*修改字典项*/
function modifyDictionary(){
	
	$('#dictionary_form_dialog').dialog({
        title: '字典--修改',
        iconCls: 'icon-book_addresses_edit',
    });
	 $("#form_dicName").combobox("readonly");
	//获取邮件点击Tree的节点编号
	var itemID=$("#hidden_selectTreeNodeId").val();
	//ajax的post方式来获取右键点击节点编号的内容
	 $.post("admin/dictionary/dic_dictionaryModifyInfo.action?dictionary.dicId=" + itemID,
		        function(data) {
		            $("#form_description").textbox('setValue', data.dictionary.description);
		            $("#form_dicName").textbox('setValue', data.dictionary.dicName);
		            $("#form_dicId_hidden").val(data.dictionary.dicId);
		        });
    
    $('#dictionary_form').form({
        url: 'admin/dictionary/dic_saveOrUpdateDictionary.action',
        onSubmit: function() {
            return $(this).form('validate');
        },
        success: function(data) {
            var json = JSON.parse(data);
            if (json.result == "Success") {
                $('#dictionary_form_dialog').dialog('close');
                $.messager.alert('提示', json.message, 'info');
                //重新加载树
                $('#dic_tree').tree('reload'); 
            } else {
                $.messager.alert('错误', json.message, 'error');
            }
        }
    });
    $('#dictionary_form_dialog').dialog('open');
	
}

//表单验证扩展验证方法
$.extend($.fn.validatebox.defaults.rules, {
    checkDicName:{//检查字典英文名称
    	validator: function (value) {
    		var checkR=$.ajax({
                async : false,  
                cache : false,
                type : 'post',  
                url : 'admin/dictionary/dic_checkDictionaryInput.action',  
                data : {  
                    'dictionary.dicName' : value,
                    'dictionary.dicId' : $("#hidden_selectTreeNodeId").val()
                } 
            }).responseText;  
    		return JSON.parse( checkR).result==="Success"; 
        },
    	message: '数据库存在此值'
    },
    checkDicDescription:{//检查字典中文名称
		validator: function (value) {
			var checkR=$.ajax({
				async : false,  
				cache : false,
				type : 'post',  
				url : 'admin/dictionary/dic_checkDictionaryInput.action',  
				data : {  
					'dictionary.description' : value,
					 'dictionary.dicId' : $("#hidden_selectTreeNodeId").val()
				} 
			}).responseText;  
			return JSON.parse( checkR).result==="Success"; 
		},
		message: '数据库存在此值'
	},
    checkDicValue:{//检查字典详情名称是否重复
		validator: function (value) {
			var item="";
			try{
				item= $('#dictionary_data').datagrid('getSelected').dicDataId;
			}catch (e) {	}
			var checkR=$.ajax({
				async : false,  
				cache : false,
				type : 'post',  
				url : 'admin/dictionary/dic_checkDictionaryDataInput.action',  
				data : {  
					'dictionaryData.dicValue' : value,
					 'dictionaryData.dictionary.dicId' : $("#hidden_selectTreeNodeId").val(),
					 'dictionaryData.dicDataId' : item
				} 
			}).responseText;  
			return JSON.parse( checkR).result==="Success"; 
		},
		message: '数据库存在此值'
	},
    checkDicKey:{//检查字典详情名称是否重复
		validator: function (value) {
			var item="";
			try{
				item= $('#dictionary_data').datagrid('getSelected').dicDataId;
			}catch (e) {	}
			var checkR=$.ajax({
				async : false,  
				cache : false,
				type : 'post',  
				url : 'admin/dictionary/dic_checkDictionaryDataInput.action',  
				data : {  
					'dictionaryData.dicKey' : value,
					 'dictionaryData.dictionary.dicId' : $("#hidden_selectTreeNodeId").val(),
					 'dictionaryData.dicDataId' : item
				} 
			}).responseText;  
			return JSON.parse( checkR).result==="Success"; 
		},
		message: '数据库存在此值'
	}
}); 

/*字典提交表单*/
function submitForm() {
    $('#dictionary_form').submit();
}
function submitDataForm() {
	$('#dictionaryData_form').submit();
}


/*清空字典表单值*/
function form_init() {
    $("#form_description").textbox('setValue', '');
    $("#form_dicName").textbox('setValue', '');
    //$("#form_dicName").combobox({readonly:false});
}


/*删除字典*/
function removeDictionary(){
	var itemID=$("#hidden_selectTreeNodeId").val();
	$.messager.confirm("操作提示", "您确定要执行删除操作吗？", function (data) {  
		if (data) {  
      
	 $.post("admin/dictionary/dic_removeDictionary.action?dictionary.dicId=" + itemID,
		        function(data) {
					 var json = data;
			         if (json.result == "Success") {
			             $.messager.alert('提示', json.message, 'info');
			             //重新加载树
			             $('#dic_tree').tree('reload'); 
			         } else {
			             $.messager.alert('错误', json.message, 'error');
			         }
		        });
		  }  
	});
}

/*格式化使用状态列，将传入值变成复选框模式*/
function dictionaryDataStatusFormatter(value, rec, index){
	var checked=rec.status==1?"checked":"";
	 return "<input id='radio_dictionaryDataStatus' name='dictionaryData.status' type='checkBox' "+checked+"  disabled=\"disabled\"/>";
}

/**
 * 添加字典详情
 */
function addDictionaryData(){
	
	$('#dictionaryData_form_dialog').dialog({
        title: '字典详情--添加',
        iconCls: 'icon-book_addresses_add',
    });
	
	//判断表格是否被选中，如果选中则取消选中
	var item=$('#dictionary_data').datagrid('selectRow');
	if(!item==null){
		$("#dictionary_data").datagrid("unselectRow", item);
	}
	
	//初始化表单值
	if(!form_dictionaryData_init()){
		return;
	}
    
    $('#dictionaryData_form').form({
        url: 'admin/dictionary/dic_saveOrUpdateDictionaryData.action',
        onSubmit: function() {
            return $(this).form('validate');
        },
        success: function(data) {
            var json = JSON.parse(data);
            if (json.result == "Success") {
                $('#dictionaryData_form_dialog').dialog('close');
                $.messager.alert('提示', json.message, 'info');
    			$("#dictionary_data").datagrid("load", {
    				"dictionary.dicId" : $("#hidden_selectTreeNodeId").val()
    			});
            } else {
                $.messager.alert('错误', json.message, 'error');
            }
        }
    });
    $('#dictionaryData_form_dialog').dialog('open');
	
}


/**
 * 初始化字典添加表单
 */
function form_dictionaryData_init(){
	var nodeId=$("#hidden_selectTreeNodeId").val();
	if(nodeId==""||nodeId<=0){
		$.messager.alert('错误', '请选择左侧要添加字典详情的字典项', 'error');
		return false;
	}
	var node=$('#dic_tree').tree('getSelected'); 
	$("#form_dicDataId").textbox('setValue', "");
	$("#form_dictionaryName").textbox('setValue', node.text);
	$("#form_dicValue").textbox('setValue', "");
	$("#form_dicKey").textbox('setValue', "");
	$("#form_remark").val("");
	$("#form_dicDataId_Hidden").val(node.id);
	return true;
}

/**
 * 删除字典详情
 */
function removeDictionaryData(){
	
	var item="";
	try{
		item=$('#dictionary_data').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要删除的详情项！', 'error');
		return;
	}
	
	$.messager.confirm("操作提示", "您确定要执行删除操作吗？", function (data) {  
		if (data) {
	$.ajax({
		data:{
			"dictionaryData.dicDataId":item.dicDataId
		},
		url:"admin/dictionary/dic_removeDictionaryData.action",
		type:'POST',
		success:function(data){
			 var json = data;
	            if (json.result == "Success") {
	                $.messager.alert('提示', json.message, 'info');
	                $("#dictionary_data").datagrid("load", {
	    				"dictionary.dicId" : $("#hidden_selectTreeNodeId").val()
	    			});
	            } else {
	                $.messager.alert('错误', json.message, 'error');
	            }
		}
	});
}
	});
}

/**
 * 修改字典详情
 */
function modifyDictionaryData(){
	
	$('#dictionaryData_form_dialog').dialog({
        title: '字典详情--修改',
        iconCls: 'icon-book_addresses_edit',
    });
	
	//判断表格是否被选中，如果选中则取消选中
	var item="";
	try{
		item=$('#dictionary_data').datagrid('getSelected');
		}catch (e) {
		}
		
	if(item==""||item==null){
		$.messager.alert('错误', '请选择要修改的详情项！', 'error');
		return;
	}
	form_dictionaryData_init();
	//初始化表单值
	$.post("admin/dictionary/dic_dictionaryDataModifyInfo.action?dictionaryData.dicDataId=" + item.dicDataId,
	        function(data) {
				
		$("#form_dicDataId").textbox('setValue', data.dictionaryData.dicDataId);
		$("#form_dictionaryName").textbox('setValue', $('#dic_tree').tree('getSelected').text);
	            $("#form_dicValue").textbox('setValue', data.dictionaryData.dicValue);
	            $("#form_dicKey").textbox('setValue', data.dictionaryData.dicKey);
	            $("#form_remark").val(data.dictionaryData.remark);
	            $("#form_status").val(data.dictionaryData.status);
	            if(data.dictionaryData.status){
		        	$("#form_status").attr("checked","checked");
		        }else{
		        	$("#form_status").removeAttr("checked");
		        }
	            $("#form_dicDataId_Hidden").val(data.dictionaryData.dictionary.dicId);
	        });
	
	
    $('#dictionaryData_form').form({
        url: 'admin/dictionary/dic_saveOrUpdateDictionaryData.action',
        onSubmit: function() {
            return $(this).form('validate');
        },
        success: function(data) {
            var json = JSON.parse(data);
            if (json.result == "Success") {
                $('#dictionaryData_form_dialog').dialog('close');
                $.messager.alert('提示', json.message, 'info');
    			$("#dictionary_data").datagrid("load", {
    				"dictionary.dicId" : $("#hidden_selectTreeNodeId").val()
    			});
            } else {
                $.messager.alert('错误', json.message, 'error');
            }
        }
    });
    $('#dictionaryData_form_dialog').dialog('open');
	
}



